unit History;

{
  PasCalc
  Version 1.2 2011-05-26

  Copyright (C) 2011 KEEPER <yar.keeper@gmail.com>

  This unit is part of the PasCalc Calculator

  This library is free software; you can redistribute it and/or
  modify it under the terms of the GNU Lesser General Public
  License as published by the Free Software Foundation; either
  version 2.1 of the License, or (at your option) any later version.

  See the file LICENSE.TXT, included in this distribution,
  for details about the copyright.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
}

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, FileUtil, LResources, Forms, Controls, Graphics, Dialogs,
  Grids, StdCtrls, Menus, Options, Windows;

type

  { TfrmHistory }

  TfrmHistory = class(TForm)
    btnExpr2Edit: TButton;
    btnAsw2Edit: TButton;
    HistList: TListBox;
    miSaveAs: TMenuItem;
    miClear: TMenuItem;
    pmHistory: TPopupMenu;
    sdHistSave: TSaveDialog;
    procedure btnAsw2EditClick(Sender: TObject);
    procedure btnExpr2EditClick(Sender: TObject);
    procedure FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
    procedure FormShow(Sender: TObject);
    procedure miClearClick(Sender: TObject);
    procedure miSaveAsClick(Sender: TObject);
  private
    { private declarations }
  public
    procedure LangInit(Str: TStringList);
  end; 

var
  frmHistory: TfrmHistory;
  edExpr: TEdit;

implementation

{ TfrmHistory }

procedure _SetFocus(Edit: TEdit; Pos: integer);
begin
  Edit.SelStart := Pos;
  Windows.SetFocus(Edit.Handle);
end;

procedure StrIns(Edit: TEdit; const str: AnsiString;
  const IsBr: boolean = false);
var
  CurPos: integer;
begin
  CurPos := Edit.SelStart;
  Edit.SelText := Str;
  if IsBr then _SetFocus(Edit, CurPos + Length(Str) - 1) else
     _SetFocus(Edit, CurPos + Length(Str));
end;

procedure TfrmHistory.FormShow(Sender: TObject);
begin
  if not fOptions.IsHistory then
  begin
    HistList.Items.Text := 'История вычислений не ведется';
    exit;
  end;
  if not FileExists(SavePath + 'history.txt') then
  begin
    HistList.Items.Add('История вычислений пока пуста');
    exit;
  end;
  HistList.Items.LoadFromFile(SavePath + 'history.txt');
  HistList.ItemIndex := HistList.Items.Count - 1;
end;

procedure TfrmHistory.miClearClick(Sender: TObject);
begin
  HistList.Items.Clear;
  HistList.Items.SaveToFile(SavePath + 'history.txt');
end;

procedure TfrmHistory.miSaveAsClick(Sender: TObject);
begin
  if sdHistSave.Execute then
    HistList.Items.SaveToFile(sdHistSave.FileName);
end;

procedure TfrmHistory.LangInit(Str: TStringList);
begin
  frmHistory.Caption := str[48];
  btnExpr2Edit.Caption := str[49];
  btnAsw2Edit.Caption := str[50]
end;

procedure TfrmHistory.btnExpr2EditClick(Sender: TObject);
var
  AnswStart: integer;
  Str: ANSIString;
begin
  Str := HistList.Items[HistList.ItemIndex];
  AnswStart := ANSIPos('=', Str) - 1;
  Str := Copy(str, 1, AnswStart);
  StrIns(edExpr, Str);
end;

procedure TfrmHistory.FormKeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
  ShowMessage('')
end;

procedure TfrmHistory.btnAsw2EditClick(Sender: TObject);
var
  AnswStart: integer;
  Str: ANSIString;
begin
  Str := HistList.Items[HistList.ItemIndex];
  AnswStart := ANSIPos('=', Str);
  Str := Copy(str, AnswStart+1, Length(Str) - AnswStart);
  StrIns(edExpr, Str);
end;

initialization
  {$I History.lrs}

end.

